package com.itheima.stock.controller;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.StockInfoExcelDTO;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.response.PageResult;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 李智勇
 * @data 2025/10/21 21:14
 */

@RestController
public class StockController implements StockControllerAPI {
    @Autowired
    private StockService stockService;

    @Override
    public ResponseResult<StockUpDownVO<InfoVO>> stickUpDown() {
        return ResponseResult.ok(stockService.stickUpDown());
    }

    @Override
    public ResponseResult<TradeAmtVO<OptionVO>> tradeAmt() {
        return ResponseResult.ok(stockService.tradeAmt());
    }

    @Override
    public ResponseResult<FileDownloadVO> export(int page, int pageSize, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(Constants.EXCEL_FILE_PREFIX + page + Constants.EXCEL_FILE_SUFFIX, StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        AtomicInteger counter = new AtomicInteger(0);
        EasyExcel.write(response.getOutputStream(),StockInfoExcelDTO.class)
                .sheet(Constants.EXCEL_FILE_PREFIX+page+Constants.EXCEL_FILE_SUFFIX)
                .doWrite(stockService.more(page, pageSize).getRows().stream().map(moreVO -> {
                    StockInfoExcelDTO stockInfoExcelDTO = new StockInfoExcelDTO();
                    BeanUtils.copyProperties(moreVO, stockInfoExcelDTO);
                    stockInfoExcelDTO.setNum(counter.incrementAndGet());
                    return stockInfoExcelDTO;
                }).toList());
        return ResponseResult.ok(FileDownloadVO.builder()
                .fileName(fileName)
                .build());
    }

    @Override
    public ResponseResult<UpDownVO<OptionVO>> upDown() {
        return ResponseResult.ok(stockService.upDown());
    }

    @Override
    public ResponseResult<PageResult<MoreVO>> more(Integer page, Integer pageSize) {
        return ResponseResult.ok(stockService.more(page, pageSize));
    }

    @Override
    public ResponseResult<List<IncreaseVO>> stockIncrease() {
        return ResponseResult.ok(stockService.stockIncrease());
    }

    //其它省略.....
    @Override
    public ResponseResult<List<SectorAllVO>> sectorAll() {
        return ResponseResult.ok(stockService.sectorAll());
    }

    /**
     * 获取国内最新大盘指数
     * @return
     */




    public StockController(StockService stockService) {
    this.stockService = stockService;
    }

    @Override
    public ResponseResult<List<InnerMarketVO>> innerIndexAll() {
        ;
        return ResponseResult.ok(stockService.innerIndexAll());
    }

    @Override
    public ResponseResult<List<StockBusiness>> getStockBusiness() {
        List<StockBusiness> stockBusiness = stockService.getStockBusiness();
        return ResponseResult.ok(stockBusiness);
    }
}